﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YJKRoomInspection.Properties;
using YJKRoomInspection.Utiltiy;
using YJKRevitKernel.FormUtility.ModelessFormControlSuite.ControlLayer;

namespace YJKRoomInspection.ShowDataForm.FrontRoomAreaCheck
{
	internal class AreaCheckModellessManger : BaseLoopMangerNoneSwitch
	{
		public AreaCheckModellessManger(YJKRoomInspectionForm inputForm) : base(inputForm.uidoc, inputForm, inputForm.RevitHandle, null)
		{
			this.m_doc = inputForm.uidoc.Document;
		}

		protected override void DoBeforeContinue()
		{
			if (this.m_useForm is YJKRoomInspectionForm)
			{
				YJKRoomInspectionForm frontRoomCheckForm = this.m_useForm as YJKRoomInspectionForm;
				if (frontRoomCheckForm.m_strOrderType == "doubleClick")
				{
					frontRoomCheckForm.DoBeforeContinue();
					return;
				}
				if (frontRoomCheckForm.m_strOrderType == "TrustAction")
				{
					frontRoomCheckForm.DoBeforeContinue();
					this.InsertData();
				}
			}
		}

		private void InsertData()
		{
			List<Element> list = new List<Element>();
			Dictionary<int, string> dictionary = new Dictionary<int, string>();
			List<InsertArea> list2 = new List<InsertArea>();
			string areaGuid = Resources.AreaGuid;
			double roomArea = 0.0;
			new List<Element>();
			new List<InsertData>();
			if (this.m_useForm is YJKRoomInspectionForm)
			{
				dictionary = (this.m_useForm as YJKRoomInspectionForm).dicTrustRoom;
				if (dictionary.Count == 0)
				{
					return;
				}
				foreach (KeyValuePair<int, string> keyValuePair in dictionary)
				{
					InsertArea insertArea = new InsertArea();
					ElementId elementId = new ElementId(keyValuePair.Key);
					list.Add(this.m_doc.GetElement(elementId));
					if (list[0] != null)
					{
						if (list[0] is Room)
						{
							roomArea = (list[0] as Room).Area;
						}
						else if (list[0] is FilledRegion)
						{
							roomArea = Utility.ChangeAreaByUnit((list[0] as FilledRegion).get_Parameter(BuiltInParameter.HOST_AREA_COMPUTED).AsDouble());
						}
						insertArea.m_RoomArea = roomArea;
						insertArea.m_BuildingType = keyValuePair.Value;
						insertArea.m_TrustNum = 1;
						insertArea.m_IsTrustElem = false;
						if (InsertDataToRoom.ReadAreaFromElement<InsertArea>(list[0], areaGuid).m_RoomArea != 0.0)
						{
							insertArea.m_IsTrustElem = true;
							insertArea.m_TrustNum = 2;
						}
						list2.Add(insertArea);
					}
					InsertDataToRoom.WriteToElement(list, Resources.AreaGuid, list2);
				}
			}
		}

		protected override void DoAfterWhile()
		{
			if (this.m_useForm is YJKRoomInspectionForm)
			{
				(this.m_useForm as YJKRoomInspectionForm).ChangeFilledRegionVisible(new List<int>());
			}
			base.DoAfterWhile();
		}

		private Document m_doc;
	}
}
